do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5

gen middleage_pre1 = inrange(win_age,30,44)

replace norm_year = norm_year + 3
	
cap rm "$table/temp/Fig3.txt"
cap rm "$table/temp/Fig3.xls"

foreach y in totbaby { 
	foreach grp in middleage{
		forv j = 0(1)1{
			reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current $control1 $control2 $control3 if `grp'_pre1 == `j', a(norm#current year age) cl(houseid)
			outreg2 using "$table/temp/Fig3.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(`grp'`j')
		}
	}
}

import delimited using "$table/temp/Fig3.txt", clear
export excel "$table/Tables.xlsx", sheet("Fig3") sheetreplace

import excel using "$table/Tables.xlsx", clear sheet(Fig3) first

keep in 4/21
keep v2 v3
gen norm = -4 + ceil(_n/2)
replace norm = norm + 1 if norm >= -1

loc N = _N + 2
set obs `N'
replace norm = -1 if norm == .

gen type = mod(_n+1,2)
reshape long v, i(norm type) j(grp)
tostring grp, replace
replace grp = "young" if y == "2"
replace grp = "middle" if y == "3"

reshape wide v, i(norm grp) j(type)

rename v0 coef
rename v1 se

foreach x in coef se{
	replace `x' = subinstr(`x',"*","",.)
	replace `x' = subinstr(`x',"(","",.)
	replace `x' = subinstr(`x',")","",.)
	destring `x', replace
	recode `x' . = 0
}

gen upper = coef + 1.96 * se
gen lower = coef - 1.96 * se


twoway 	(connect coef norm if grp == "young", ms(O) color(black)) ///
		(rline upper lower norm if grp == "young", lp(dash) color(gs8)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle(Years from Lottery Prize) xlabel(-3(1)6) ///
		ytitle(Effect on Cumulative Number of Children) ylabel(, angle(0) format(%4.2f)) ///
		scheme(s1color) yline(0, lc(black))
		
graph export "$folder/figure/Fig3a.eps", as(eps) replace


twoway 	(connect coef norm if grp == "middle", ms(O) color(black)) ///
		(rline upper lower norm if grp == "middle", lp(dash) color(gs8)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle(Years from Lottery Prize) xlabel(-3(1)6) ///
		ytitle(Effect on Cumulative Number of Children) ylabel(, angle(0) format(%4.2f)) ///
		scheme(s1color) yline(0, lc(black))
		
graph export "$folder/figure/Fig3b.eps", as(eps) replace
